package com.deyuanyun.pic.settings.service.msgtemplate.pbf;

import com.deyuanyun.pic.common.util.CommonUtil;
import com.deyuanyun.pic.common.util.StringUtils;
import com.deyuanyun.pic.settings.controller.dto.BizMsgThresholdVO;
import com.deyuanyun.pic.settings.controller.dto.base.RepairTaskDTO;
import com.deyuanyun.pic.settings.domain.base.BizMsgUser;
import com.deyuanyun.pic.settings.enumeration.ThresholdType;
import com.deyuanyun.pic.settings.service.msgtemplate.AbstractPushMsgTemplate;
import com.deyuanyun.pic.settings.service.msgtemplate.MsgQueue;
import com.google.common.collect.Lists;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 *  维修项目消息推送
 *  @author axiao(xj08.dream@gmail.com)
 *  @date 2016-08-12
 *  @version 1.0
 *  Histroy: [修改人][时间][描述]
 */
public class RepairProjectPushMsg extends AbstractPushMsgTemplate {

    private static final Logger LOGGER = LoggerFactory.getLogger(RepairProjectPushMsg.class);

    /** 维修任务id key */
    public static final String REPAIR_IDS = "rtIds";
    /** 维修任务状态 key，其中包含已发布，已结束(为已维修) */
    public static final String REPAIR_FLAG = "repairFlag";
    /** 创建备注 */
    private String createRemark;

    @Override
    public void doPushMsgData() {
        if(super.interfaceParam.isEmpty()) {
            LOGGER.info("没有传入参数");
            return;
        }
        Map inteMap = super.interfaceParam;
        Object rtIdsObj = inteMap.get(REPAIR_IDS);
        String repairFlag = (String) inteMap.get(REPAIR_FLAG);
        if(rtIdsObj == null || repairFlag == null) {//其中任何参数任何一个为空都不允许向下执行
            LOGGER.info("参数有空值存在，不允许向下执行");
            return;
        }
        String[] rtIds = (String[]) rtIdsObj;
        List<RepairTaskDTO> taskDTOs =  super.bizMsgThresholdService.queryRepairByIds(rtIds);//根据传入的维修任务id数组得到缺陷信息
        BizMsgThresholdVO repairTaskVO = super.bizMsgThresholdService.queryDataByTypeOne(ThresholdType.DEFECT_REPAIR_TASKS.getValue());//得到维修维护项目阀值设置信息

        for (RepairTaskDTO taskDTO : taskDTOs) {
            super.roleList = repairTaskVO.getRoleIdList();
            this.createRemark = "维修维护项目" + repairFlag;
            LOGGER.info(this.createRemark);
            super.thresListCastString(Lists.newArrayList(taskDTO.getName(), repairFlag));
            initializeBizMsgSends(taskDTO, repairTaskVO, repairFlag);
        }
    }

    /**
     * @param args 参数说明：
     * <pre>
     * args[0]：为维修任务信息，类型为：{@link RepairTaskDTO}<br />
     * args[1]：为业务阀值实体信息，类型为：{@link BizMsgThresholdVO}<br />
     * args[2]：为维修任务状态，有已发布或者已关闭，类型为：{@link String}<br />
     * </pre>
     */
    @Override
    public void initializeBizMsgSends(Object... args) {
        RepairTaskDTO taskDTO = (RepairTaskDTO) args[0];
        BizMsgThresholdVO repairTaskVO = (BizMsgThresholdVO) args[1];
        String repairFlag = (String) args[2];
        String thresholdId = repairTaskVO.getId();
        String msgContent = repairTaskVO.getMsgContent();

        if("已启动".equals(repairFlag) || ("已关闭".equals(repairFlag) && "ALREADY_REPAIR".equals(taskDTO.getRepairState()))) {
            if("已启动".equals(repairFlag))
                LOGGER.info("推送已启动维修任务消息,任务id为：" + taskDTO.getId());
            if("已关闭".equals(repairFlag) && "ALREADY_REPAIR".equals(taskDTO.getRepairState()))
                LOGGER.info("推送已关闭维修任务消息,任务id为：" + taskDTO.getId());

            if(super.roleList != null && !super.roleList.isEmpty()) {
                Set<String> userIds = super.bizMsgThresholdService.queryUseIdByRoleIds(roleList); //用户id集合
                List<MsgQueue.BizMsgUserInfo> userInfos = new ArrayList<MsgQueue.BizMsgUserInfo>();
                for (String userId : userIds) {
                    BizMsgUser msgUser = new BizMsgUser();
                    msgUser.setCreateRemark(this.createRemark);
                    if (StringUtils.isNotEmpty(msgContent))//消息内容不为空的时候设置到消息推送实体类
                        msgUser.setMsgContent(msgContent);
                    if (StringUtils.isNotEmpty(super.thresholdContent)) { //设置阀值内容
                        msgUser.setThresholdContent(super.thresholdContent);
                    }
                    msgUser.setUserId(userId);
                    msgUser.setId(CommonUtil.getUUID());
                    msgUser.setCreateDate(new Date());

                    userInfos.add(msgQueue.new BizMsgUserInfo().setValid(false).setBizMsgUser(msgUser));
                }
                if(!userInfos.isEmpty()) {
                    MsgQueue.MsgInfo msgInfo = msgQueue.new MsgInfo().setBizMsgThresholdId(thresholdId)
                            .setThresholdSet(repairTaskVO.getThresholdContent()).setRoleIds(super.roleList);
                    msgQueue.addOperator(msgQueue.new PushMsgOperator(userInfos, msgInfo));
                }
            }
        }
    }

    @Override
    public boolean valiIsPush(Map<String, Object> valiMap) {
        return false;
    }
}
